package com.baway.demo_recyclerview

import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.gson.Gson
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response

/**
 * 一.recyclerview四级缓存：
 * 1.屏幕内缓存 屏幕内缓存指在屏幕中显示的ViewHolder，在快速滑动时屏幕内viewholder的复用
 * 2.屏幕外缓存 当列表滑动出了屏幕时，缓存的ViewHolder默认是2个：mCacheViews
 * 3.RecycledViewPool类是用来缓存ViewHolder，一种item类型存储5个viewHolder。
 * 多个页面的recyclerview可以共享viewholder，可以通过setRecycledViewPool(RecycledViewPool)设置同一个RecycledViewPool，他们就可以共享ViewHolder
 * 4.开发者可自定义缓存，是虚拟类ViewCacheExtension的一个实例，开发者可实现方法getViewForPositionAndType(Recycler recycler, int position, int type)来实现自己的缓存
 * 二.recyclerview和listview区别
 * 三.recyclerView优化
 */
class MainActivity : AppCompatActivity() {
    private lateinit var rv:RecyclerView
    private lateinit var adapter:VideoAdapter
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        rv = findViewById(R.id.rv)
        adapter = VideoAdapter()
        rv.adapter  = adapter
        rv.layoutManager = LinearLayoutManager(this)

        OkGo.get<String>("http://10.161.9.80:7015/videosimple/getRecommendSimpleVideo?page=1&pagesize=40").execute(object : com.lzy.okgo.callback.StringCallback(){
            override fun onSuccess(response: Response<String>?) {
                val body = response?.body()
                val videoEntity = Gson().fromJson(body, VideoEntity::class.java)
                val data = (videoEntity.data) as MutableList<Data>

                adapter.setList(data)

            }

        })
    }

    fun add(view: View) {
        val data = Data(description = "这是我新家的数据",
            videomainimag = "http://js2.a.yximgs.com/upic/2020/08/23/20/BMjAyMDA4MjMyMDU3MzVfNDc1MTQ4NzZfMzQ4NzcwMzIxOTlfMV8z_low_B64a70bfe85ad9a7adb6107fe37ff8cb3.webp?tag=1-1599270586-vdg-0-0kmqidk9iz-0a89305dbf9d0556&bp=10000")
        adapter.addBean(0,data)

    }
    fun remove(view: View) {
        adapter.removeBean(0)
    }
    fun update(view: View) {
        val data = Data(description = "我修改完的新的数据",
            videomainimag = "http://js2.a.yximgs.com/upic/2020/08/23/20/BMjAyMDA4MjMyMDU3MzVfNDc1MTQ4NzZfMzQ4NzcwMzIxOTlfMV8z_low_B64a70bfe85ad9a7adb6107fe37ff8cb3.webp?tag=1-1599270586-vdg-0-0kmqidk9iz-0a89305dbf9d0556&bp=10000")
        adapter.updateBean(0,data)
    }

    fun updateText(view: View) {
        val data = Data(description = "我做了局部增量刷新",videomainimag = "http://js2.a.yximgs.com/upic/2020/08/23/20/BMjAyMDA4MjMyMDU3MzVfNDc1MTQ4NzZfMzQ4NzcwMzIxOTlfMV8z_low_B64a70bfe85ad9a7adb6107fe37ff8cb3.webp?tag=1-1599270586-vdg-0-0kmqidk9iz-0a89305dbf9d0556&bp=10000")
        adapter.updateBean(0,data,"text")
    }
}